package cn.hh.ymcc.mq.consumer;


import cn.hh.ymcc.dto.PyaResultDto;
import cn.hh.ymcc.service.ICourseOrderService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
// 交给容器管理   consumerGroup = 消费者组名    topic = 生产者的中topic  selectorExpression = 生产者中的tags   messageModel.BROADCASTING = 消费消息的模式是广播: 一个消费可以被多个人同时消费
@RocketMQMessageListener(consumerGroup = "service-order-payTimeOut-consumer", topic = "topic-paytimeout", selectorExpression = "tag-paytimeout", messageModel = MessageModel.BROADCASTING)
public class PayTimeOutOrderCancelConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private ICourseOrderService courseOrderService;

    /**
     * 消费消息
     *  只要不报错,就会自动签收消息
     * @param messageExt
     */
    @Override
    public void onMessage(MessageExt messageExt) {  // 这里收到的消息体对象是PyaResultDto
        if (messageExt.getBody() == null) { // 防御性编程
            return;
        }
        // 真正消费消息的业务逻辑
        // 调用messageExt.getBody()获取到消息体, 将获取到的消息体转成字符串类型对象jsonStr
        String orderNo = new String(messageExt.getBody(), StandardCharsets.UTF_8);

        // 处理超时订单,修改订单支付状态为取消
        courseOrderService.payTimeOutCancelOrder(orderNo);

    }

}
